package com.imageim.yimirror.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.imageim.yimirror.R;
import com.imageim.yimirror.bean.imageManage.ImagePhoto;
import com.imageim.yimirror.common.ui.GlideUtils;
import com.imageim.yimirror.common.util.ToastUtil;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Bruce.Zhou on 2018/6/26 17:52.
 * Email: 907160968@qq.com
 */

public class DailyPhotoViewAdapter extends RecyclerView.Adapter<DailyPhotoViewAdapter.MyHolder> {

    private Context context;
    private List<ImagePhoto.ClothingResultBean> mData;
    private OnItemClickListener mOnItemClickListener;
    private static int red = Color.parseColor("#ee3333");
    private int clickItemNum = 0;
    private HashSet<String> checkItemPhotoNoSet = new HashSet<>();

    public DailyPhotoViewAdapter(Context context, List<ImagePhoto.ClothingResultBean> list) {
        this.context = context;
        this.mData = list;
    }

    public void setData(List<ImagePhoto.ClothingResultBean> list) {
        mData = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_horizontal_photo_daily_new, null);
        return new MyHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolder holder, final int position) {
        final ImagePhoto.ClothingResultBean bean = mData.get(position);
        GlideUtils.display(holder.ivPhoto, bean.getUrl());
        if (mOnItemClickListener != null) {
            holder.ivPhoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //                    unCheckItems();
                    if (mData != null && position < mData.size()) {
                        if (!bean.isChecked()) {
                            if (clickItemNum < 2) {
                                if (!checkItemPhotoNoSet.contains(mData.get(position).getPhotoNo())) {
                                    checkItemPhotoNoSet.add(mData.get(position).getPhotoNo());
                                    clickItemNum++;
                                    bean.setChecked(true);
                                    notifyDataSetChanged();
                                    mOnItemClickListener.onClickPhoto(position, mData.get(position).getPhotoNo(), true);
                                }
                            }
                        } else {
                            //如果选中  取消选中
                            bean.setChecked(false);
                            removeCheckItem(mData.get(position).getPhotoNo());
                            clickItemNum--;
                            notifyDataSetChanged();
                            mOnItemClickListener.onClickPhoto(position, mData.get(position).getPhotoNo(), false);
                        }
                    }
                }
            });

            holder.ivPhoto.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    if (clickItemNum < 2) {
                        if (!bean.isChecked()) {
                            mOnItemClickListener.onLongClickPhoto((ImageView) view, position);
                        } else {
                            ToastUtil.show(R.string.compare_select_same);
                        }
                    }
                    return false;
                }
            });

            holder.ivDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onClickDelete(position);
                }
            });

        }
        holder.tvDate.setText(bean.getTime().substring(5, 10).replace("-", "/"));
        String year = bean.getTime().substring(0, 4);
        if (position == mData.size() - 1 || !year.equals(mData.get(position + 1).getTime().substring(0, 4))) {
            holder.lineView.setVisibility(View.VISIBLE);
            holder.tvYear.setText(year);
        } else {
            holder.lineView.setVisibility(View.GONE);
        }

        if (bean.isChecked()) {
            holder.ivPhoto.setBorderColor(red);
        } else {
            holder.ivPhoto.setBorderColor(Color.TRANSPARENT);
        }
    }

    private void removeCheckItem(String photoNo) {
        Iterator<String> iterator = checkItemPhotoNoSet.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            if (next.equals(photoNo)) {
                iterator.remove();
            }
        }
    }


    public void checkItemAndNotify() {
        if (checkItemPhotoNoSet != null && checkItemPhotoNoSet.size() > 0) {
            for (String item : checkItemPhotoNoSet) {
                for (int i = 0; i < mData.size(); i++) {
                    if (item.equals(mData.get(i).getPhotoNo())) {
                        mData.get(i).setChecked(true);
                    }
                }
            }

            notifyDataSetChanged();
        }
    }

    private void unCheckItems() {
        for (ImagePhoto.ClothingResultBean bean : mData) {
            bean.setChecked(false);
        }
    }

    public void unCheckAllItemAndNotify() {
        for (ImagePhoto.ClothingResultBean bean : mData) {
            bean.setChecked(false);
        }

        checkItemPhotoNoSet.clear();
        clickItemNum = 0;
        notifyDataSetChanged();
    }

    public void unCheckItemAndNotify(String photoNo) {
        if (checkItemPhotoNoSet.contains(photoNo)) {
            for (int i = 0; i < mData.size(); i++) {
                if (photoNo.equals(mData.get(i).getPhotoNo())) {
                    mData.get(i).setChecked(false);
                    removeCheckItem(photoNo);
                    if (clickItemNum != 0) {
                        clickItemNum--;
                    }
                    break;
                }
            }

            notifyDataSetChanged();
        }
    }

    public void setSelectItem(String photoNo) {
        if (clickItemNum >= 2) {
            return;
        }
        for (int i = 0; i < mData.size(); i++) {
            if (photoNo.equals(mData.get(i).getPhotoNo())) {
                mData.get(i).setChecked(true);
                checkItemPhotoNoSet.add(photoNo);
                if (clickItemNum < 2) {
                    clickItemNum++;
                }
            }
        }

        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    static class MyHolder extends RecyclerView.ViewHolder {
        RoundedImageView ivPhoto;
        ImageView ivDelete;
        View lineView;
        TextView tvYear;
        TextView tvDate;

        public MyHolder(View itemView) {
            super(itemView);
            ivPhoto = itemView.findViewById(R.id.iv_photo);
            ivDelete = itemView.findViewById(R.id.iv_delete);
            lineView = itemView.findViewById(R.id.line_view);
            tvYear = itemView.findViewById(R.id.tv_year);
            tvDate = itemView.findViewById(R.id.tv_date);
        }
    }

    public interface OnItemClickListener {
        void onClickPhoto(int position, String photoNo, boolean isCheck);

        void onClickDelete(int position);

        void onLongClickPhoto(ImageView imageView, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }
}